package com.jhgsys.internal.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jhgsys.internal.base.service.IBaseGoodsService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.base.entity.BaseGoods;
import com.jhgsys.internal.dict.entity.DictItem;
import com.jhgsys.internal.dict.service.IDictItemService;
import com.jhgsys.internal.system.entity.Tenant;
import com.jhgsys.internal.system.service.ITenantService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 商品表 Controller
 *
 * @author admin
 * @date 2024-08-06 09:41:04
 */
@Slf4j
@Validated
@Controller
public class BaseGoodsController extends BaseController {

    @Autowired
    private IBaseGoodsService baseGoodsService;

    @Autowired
    private ITenantService tenantService;



    @GetMapping(FebsConstant.VIEW_PREFIX + "baseGoods")
    @RequiresPermissions("baseGoods:view")
    public String baseGoodsIndex(Model model){
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("tenantList",getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        return FebsUtil.view("base/baseGoods/baseGoodsList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseGoods/add")
    @RequiresPermissions("baseGoods:add")
    public String baseGoodsAdd(){
        return FebsUtil.view("base/baseGoods/baseGoodsEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseGoods/update/{id}")
    @RequiresPermissions("baseGoods:edit")
    public String baseGoodsUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("baseGoods", baseGoodsService.findBaseGoodsById(id));
        return FebsUtil.view("base/baseGoods/baseGoodsEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseGoods/detail/{id}")
    @RequiresPermissions("baseGoods:view")
    public String baseGoodsDetail(Model model, @PathVariable("id") String id){
        BaseGoods goods = baseGoodsService.findBaseGoodsById(id);
        model.addAttribute("goods", goods);
        Tenant tenant = tenantService.findTenantByTenantId(goods.getTenantId());
        model.addAttribute("tenant", tenant);
        return FebsUtil.view("base/baseGoods/baeGoodsDetail");
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @GetMapping("baseGoods")
    @ResponseBody
    @RequiresPermissions("baseGoods:view")
    public FebsResponse getAllBaseGoodss(BaseGoods baseGoods) {
        return new FebsResponse().success().data(baseGoodsService.findBaseGoodsList(baseGoods));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @GetMapping("baseGoods/list")
    @ResponseBody
    @RequiresPermissions("baseGoods:view")
    public FebsResponse baseGoodsList(QueryRequest request, BaseGoods baseGoods) {
        IPage<BaseGoods> page = this.baseGoodsService.findBaseGoodsPageWithDicName(request, baseGoods);
        return new FebsResponse().success().data(getDataTable(page));
    }

    /**
     * 新增 BaseGoods
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @Log("新增BaseGoods")
    @PostMapping("baseGoods")
    @ResponseBody
    @RequiresPermissions("baseGoods:add")
    public FebsResponse addBaseGoods(@RequestBody BaseGoods baseGoods) throws FebsException {
        try {
            baseGoods.setCreatedBy(getCurrentUser().getUserName());
            baseGoods.setDeletedFlg(FebsConstant.VALID);
            this.baseGoodsService.createBaseGoods(baseGoods);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增BaseGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }


    /**
     * 逻辑删除 BaseGoods
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @Log("逻辑删除BaseGoods")
    @GetMapping("baseGoods/delete/{id}")
    @ResponseBody
    @RequiresPermissions("baseGoods:delete")
    public FebsResponse deleteBaseGoods(@PathVariable("id") String baseGoodsIds) throws FebsException {
        try {
            BaseGoods baseGoods = new BaseGoods();
            baseGoods.setDeletedBy(getCurrentUser().getUserName());
            baseGoods.setDeletedTime(new Date());
            baseGoods.setDeletedFlg(FebsConstant.INVALID);
            this.baseGoodsService.deleteBaseGoods(baseGoods,baseGoodsIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 BaseGoods
    * @author admin
    * @date 2024-08-06 09:41:04
    */
    @Log("物理删除BaseGoods")
    @GetMapping("baseGoods/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("baseGoods:delete")
    public FebsResponse physicallyDeleteBaseGoods(@PathVariable("id") String baseGoodsIds) throws FebsException {
        try {
            this.baseGoodsService.physicallyDeleteBaseGoods(baseGoodsIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 BaseGoods
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @Log("修改BaseGoods")
    @PostMapping("baseGoods/update")
    @ResponseBody
    @RequiresPermissions("baseGoods:edit")
    public FebsResponse updateBaseGoods(@RequestBody BaseGoods baseGoods) throws FebsException {
        try {
            baseGoods.setUpdatedBy(getCurrentUser().getUserName());
            this.baseGoodsService.updateBaseGoods(baseGoods);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改BaseGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 BaseGoods
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @Log("修改BaseGoods状态")
    @PostMapping("baseGoods/updateStatus")
    @ResponseBody
    @RequiresPermissions("baseGoods:edit")
    public FebsResponse updateBaseStatus(BaseGoods baseGoods) throws FebsException {
        try {
            baseGoods.setUpdatedBy(getCurrentUser().getUserName());
            this.baseGoodsService.updateBaseGoods(baseGoods);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改BaseGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }



    /**
     * 导出 BaseGoods的excel
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @PostMapping("baseGoods/excel")
    @ResponseBody
    @RequiresPermissions("baseGoods:view")
    public void export(QueryRequest queryRequest, BaseGoods baseGoods, HttpServletResponse response) throws FebsException {
        try {
            List<BaseGoods> baseGoodss = this.baseGoodsService.findBaseGoodsPage(queryRequest, baseGoods).getRecords();
            ExcelKit.$Export(BaseGoods.class, response).downXlsx(baseGoodss, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
